# -*- coding: utf-8 -*-
import struct
GETMODE    = 0x10000001		# 得到封装类型
GETLENGTH	= 0x10000002		# 得到封装类型的长度
GETCTRL	= 0x10000003		#得到控制消息
GETJMSG    = 0x10000004		# 得到J消息
PPLI_2_2	= 0x10000005		# PPLI2.2
PPLI_2_5	= 0x10000006		# PPLI2.5
REALDISPLAY =    0x10000007	# 实时消息
COMMONDISPLAY	= 0x10000008	# 普通消息
COUNTDISPLAY	= 0x10000009	# 消息统计
OPENCHECK		= 0x10000010	# 开机自检
HARDWARE		= 0x10000011	# 硬件结果
ADDSLOT		=	12			# 增加时隙延时

# 信息终端->模拟器
HANDSHAKEINQ = 0x20000001			#PC->端机握手
SELFTESTCTL	 = 0x20000007			# PC->端机自检
INITPARA	 = 0x20000003			# PC->端机初始化
SLOTTXDATA	 = 0x20000005			# PC->端机时隙参数
POWERSWITCH  = 0x2000000E			# PC->端机开关
RETURNFLAG	 = 0x20000000			# PC退出标志位
RESYSTEMINFO = 0x20000004          # PC重新配置系统信息
# 模拟器->信息终端
HANDSHAKEACK = 0x60000001			# 端机->PC回复握手信息
SELFTESTACK	 = 0x60000007			# 端机->PC回复自检确认
SELFTESTDATA = 0x40000001			# 端机->PC自检结果统计
INITACK		 = 0x60000003			# 端机->PC回复初始化应答
JUSTATE		 = 0x60000004			# 端机->PC发送端机运行状态
JUMSG		 = 0x60000005			# 端机->PC空口消息
HWSTATE		 = 0x40000002			# 端机->PC上报硬件状态
JUNETMSG	 = 0x6000000A			# 端机->PC上报入网过程
SWITCHPOWER  = 0x6000000E			# 端机->PC开关
VERSIONMSG	 = 0x6000000B			# 端机->PCVersion版本号

class HeaderAck:
    def __init__(self):
        self.struMsgHeader = 0
        self.formart = 'I'
        self.size = 4
    def pack(self):
        return struct.pack(self.formart,self.struMsgHeader)
    def unpack(self,recdata):
        results = struct.unpack(self.formart,recdata)
        self.struMsgHeader = results[0]

class PversionMsg:
    def __init__(self):
        self.struMsgHeader = 0
        self.uVersionMsg = []
        self.format = 'I5I'
        self.size = 24
    def pack(self):
        return struct.pack(self.format,self.struMsgHeader,self.uVersionMsg[0],self.uVersionMsg[1],self.uVersionMsg[2],self.uVersionMsg[3],self.uVersionMsg[4])
    def unpack(self,recdata):
        results = struct.unpack(self.format,recdata)
        self.struMsgHeader = results[0]
        self.uVersionMsg = results[1:6]

#系统时间
class SysTime:
    def __init__(self):
        self.ucEpochNum = 0  #时元
        self.ucFrameNum = 0  #时帧
        self.usSlotNum = 0   #时隙
        self.format = 'BBH'
        self.size = 4
    def pack(self):
        return struct.pack(self.format,self.ucEpochNum,self.ucFrameNum,self.usSlotNum)
    def unpack(self,recdata):
        results = struct.unpack(self.format,recdata)
        self.ucEpochNum = results[0]
        self.ucFrameNum = results[1]
        self.usSlotNum = results[2]

#时隙设置
class SlotSrc:
    def __init__(self):
        self.ucSetNum = 0  #组号0:A 1:B 3:C
        self.ucIndex  = 0  #时元内时隙号
        self.ucRcn    = 0  #重复率
        self.format = 'BHBx'
        self.size = 6
    def pack(self):
        return struct.pack(self.format,self.ucSetNum,self.ucIndex,self.ucRcn)

    def unpack(self,recdata):
        results = struct.unpack(self.format,recdata)
        self.ucSetNum = results[0]
        self.ucIndex  = results[1]
        self.ucRcn    = results[2]

class RttInit:
    def __init__(self):
        self.ucRttItype = 0  #RTT模式
        self.usRttIpara = 0  #RTTA时表示目标源航迹号 ，RTTB时表示时间质量
        self.format = 'BH'
        self.size = 4

    def pack(self):
        return struct.pack(self.format, self.ucRttItype, self.usRttIpara)

    def unpack(self, recdata):
        results = struct.unpack(self.format, recdata)
        self.ucRttItype = results[0]
        self.usRttIpara = results[1]

# 模拟器上报开机自检结构
class Juinitack:
    def __init__(self):
        self.struMsgHeader = 0
        self.ucJUSelfTestRes = [0]*10  #模拟结果
        self.format = 'I10Bxx'
        self.size = 16

    def pack(self):
        params = []
        params.append(self.struMsgHeader)
        params.extend(self.ucJUSelfTestRes)
        return struct.pack(self.format, *params)

    def unpack(self, recdata):
        results = struct.unpack(self.format, recdata)
        self.struMsgHeader = results[0]
        self.ucJUSelfTestRes = results[1:14]

# 模拟器运行中硬件状态
class HwState:
    def __init__(self):
        self.struMsgHeader = 0
        self.ucJUHwState = [0]*30
        self.format = 'I30Bxx'
        self.size = 36

    def pack(self):
        params = []
        params.append(self.struMsgHeader)
        params.extend(self.ucJUHwState)
        return struct.pack(self.format, *params)

    def unpack(self, recdata):
        results = struct.unpack(self.format, recdata)
        self.struMsgHeader = results[0]
        self.ucJUHwState = results[1:34]

# 开关状态 STR_HW_GNFIG
class StPower:
    def __init__(self):
        self.struMsgHeader = 0
        self.uiHwPower = 0  #开关标志位 0表示关，1表示开
        self.format = 'II'
        self.size = 8

    def pack(self):
        return struct.pack(self.format, self.struMsgHeader, self.uiHwPower)

    def unpack(self, recdata):
        results = struct.unpack(self.format, recdata)
        self.struMsgHeader = results[0]
        self.uiHwPower = results[1]

# main_parameter
class MainParameter:
    def __init__(self):
        self.usIndex = 0    #封装模式
        self.srcTrackNumber = 0  #源航迹号
        self.SDUnumber = 0       #SDU序列号
        self.type = 0            #测试模式
        self.mode = 0            # 封装模式
        self.n = 0
        self.m = 0
        self.destTrackNumber = 0 #目的源航迹号
        self.RT_TM = 0;          #0为J消息 1为音频
        self.E = [0] * 2
        self.C = [0] * 7
        self.format = 'iiiiiiiii2B7Bxxx'
        self.size = 48

    def pack(self):
        params = [self.usIndex,self.srcTrackNumber,self.SDUnumber,self.type,self.mode,self.n,self.m,self.destTrackNumber,self.RT_TM]
        params.extend(self.E)
        params.extend(self.C)
        return struct.pack(self.format, *params)

    def unpack(self, recdata):
        results = struct.unpack(self.format, recdata)
        self.usIndex = results[0]
        self.srcTrackNumber = results[1]
        self.SDUnumber = results[2]
        self.type = results[3]
        self.mode = results[4]
        self.n = results[5]
        self.m = results[6]
        self.destTrackNumber = results[7]
        self.RT_TM = results[8]
        self.E = results[36:38]
        self.C = results[38:45]


class PPLI2_2:
    def __init__(self):
        self.time = 0        #时间质量
        self.hight = 0       #高度
        self.latitude = 0    #纬度
        self.longitude = 0   #经度
        self.azimuth = 0     #航向
        self.speed = 0       # 速度
        self.format = 'idddii'
        self.size = 40

    def pack(self):
        return struct.pack(self.format, self.time, self.hight,self.latitude,self.longitude,self.azimuth,self.speed)

    def unpack(self, recdata):
        results = struct.unpack(self.format, recdata)
        self.time = results[0]
        self.hight = results[1]
        self.latitude = results[2]
        self.longitude = results[3]
        self.azimuth = results[4]
        self.speed = results[5]

class PPLI2_5:
    def __init__(self):
        self.location = 0        #在延长字还是继续字有位置信息
        self.time = 0            #时间质量
        self.hight = 0           #海拔
        self.latitude = 0        #
        self.longitude = 0     #航向
        self.identity = 0       # 速度
        self.action = 0
        self.format = 'idddii'
        self.size = 40

    def pack(self):
        return struct.pack(self.format, self.time, self.hight,self.latitude,self.longitude,self.azimuth,self.speed)

    def unpack(self, recdata):
        results = struct.unpack(self.format, recdata)
        self.location = results[0]
        self.time = results[1]
        self.hight = results[2]
        self.latitude = results[3]
        self.longitude = results[4]
        self.identity= results[5]
        self.action = results[6]
class Message_rcv:
    def __init__(self):
        self.srcTrackNumber = 0 #原地址
        self.mode = 0 #封装模式
        self.SN = 0 #序列号
        self.n = 0
        self.m = 0
        self.len = 0 #长度
        self.RT_TM = 0 #
        self.destTrackNumber = 0 #目的地址
        self.SDUNumber = 0
        self.format = 'iiiiiiiii'
        self.size = 36
    def pack(self):
        return struct.pack(self.format, self.srcTrackNumber, self.mode, self.SN, self.n, self.m, self.len,self.RT_TM,self.destTrackNumber,self.SDUNumber)

    def unpack(self, recdata):
        results = struct.unpack(self.format, recdata)
        self.srcTrackNumber = results[0]
        self.mode = results[1]
        self.SN = results[2]
        self.n = results[3]
        self.m = results[4]
        self.len = results[5]
        self.RT_TM = results[6]
        self.destTrackNumber =results[7]
        self.SDUNumber = results[8]


class Message_Send_Data:
    def __init__(self):
        self.Message_Data = ''


class StruTxdata:
    def __init__(self):
        self.struMsgHeader = 0
        self.strTxSlotSrc = 0 # 该消息发送时隙号
        self.uiTxMsgNo = 0  # 发送消息序列号
        self.uiTxMsgLength = 0; # 时隙消息所占字节数
        self.ucTxMsgData = [0]*379 # J消息（报头 + 内容）
        self.format = 'IBBHHH379Bx'

    def pack(self):
        params = [self.struMsgHeader,self.strTxSlotSrc,self.uiTxMsgNo,self.uiTxMsgLength]
        params.extend(self.ucTxMsgData)
        return struct.pack(self.format,*params)

    def unpack(self,recvdata):
        results = struct.unpack(self.format,recvdata)
        self.struMsgHeader = results[0]
        self.strTxSlotSrc = results[1]
        self.uiTxMsgNo = results[2]
        self.uiTxMsgLength = results[3]
        self.ucTxMsgData = results[4:-2]

class SysWorkTime:
    def __init__(self):
        self.struMsgHeader = 0
        self.ucWorkMode = 0         #工作模式 0 ：正常模式
        self.ucNtrIndicator = 0     #指示是否是NTR 1:是 0：不是
        self.strRttI  = RttInit()
        self.ucTransMode = 0       #通信模式 跳频0
        self.ucFreqNum   =0       #单频 0-50
        self.srcTrackNum = 0       #源航迹号
        self.usSduNum    = 0        #SDU序列号
        self.strSysTime = SysTime() #系统时间
        self.strRttSlot = SlotSrc() #RTT时隙资源
        self.ucFreqSeed  = 0           #跳频种子
        self.ucJitterSeed  =0         #跳时种子
        self.format = 'IBBBHBBHHBBHBHBxBB'
        self.size = 28
    def pack(self):
        params = []
        params = [self.struMsgHeader,self.ucWorkMode,self.ucNtrIndicator,self.strRttI.ucRttItype,self.strRttI.usRttIpara, \
                  self.ucTransMode,self.ucFreqNum,self.srcTrackNum,self.srcTrackNum,self.strSysTime.ucEpochNum,self.strSysTime.ucFrameNum,self.strSysTime.usSlotNum,\
                  self.strRttSlot.ucSetNum,self.strRttSlot.ucIndex,self.strRttSlot.ucRcn,self.ucFreqSeed,self.ucJitterSeed]
        return struct.pack(self.format,*params)

    def unpack(self,recvdata):
        results = struct.unpack(self.format,recvdata)
        self.struMsgHeader = results[0]
        self.ucWorkMode = results[1]
        self.ucNtrIndicator = results[2]
        self.strRttI.ucRttItype = results[3]
        self.strRttI.usRttIpara = results[4]
        self.ucTransMode = results[5]
        self.ucFreqNum = results[6]
        self.srcTrackNum = results[7]
        self.strSysTime.ucEpochNum = results[8]
        self.strSysTime.ucFrameNum = results[9]
        self.strSysTime.usSlotNum = results[10]
        self.strRttSlot.ucSetNum = results[11]
        self.strRttSlot.ucIndex  = results[12]
        self.strRttSlot.ucRcn = results[13]
        self.ucFreqSeed = results[14]
        self.ucJitterSeed = results[15]

class JustateData:
    def __init__(self):
        self.struMsgHeader = 0
        self.strNetTime = SysTime()  #网络时间
        self.ucJUState = ''          #模拟器状态
        self.ucTimeQual = ''         #时间质量
        self.ucMsgSpace = 0          #剩余消息空间
        self.uiErrorCode = 0         #错误代码
        self.format = 'IBBHBBBI'
        self.size = 16
    def pack(self):
        params = [self.struMsgHeader,self.strNetTime.ucEpochNum,self.strNetTime.ucFrameNum,self.strNetTime.usSlotNum, \
                  self.ucJUState,self.ucTimeQual,self.ucMsgSpace,self.uiErrorCode]
        return struct.pack(self.format,*params)
    def unpack(self,recvdata):
        results = struct.unpack(self.format,recvdata)
        self.struMsgHeader = results[0]
        self.strNetTime.ucEpochNum = results[1]
        self.strNetTime.ucFrameNum = results[2]
        self.strNetTime.usSlotNum = results[3]
        self.ucJUState = results[4]
        self.ucTimeQual = results[5]
        self.ucMsgSpace = results[6]
        self.uiErrorCode = results[7]


class StruNetMsg:
    def __init__(self):
        self.struMsgHeader = 0
        self.strNetTime = SysTime()
        self.uiMsgNo = 0
        self.usRevMsgLength = 0
        self.ucJURxMsg = [0]*379
        self.format = 'IBBHHH379Bx'
        self.size = 392
    def pack(self):
        params = [self.struMsgHeader,self.strNetTime.ucEpochNum,self.strNetTime.ucFrameNum,self.strNetTime.usSlotNum,self.uiMsgNo,self.usRevMsgLength]
        params.extend(self.ucJURxMsg)
        return struct.unpack(self.format,*params)
    def unpack(self,recvdata):
        results = struct.unpack(self.format,recvdata)
        self.struMsgHeader = results[0]
        self.strNetTime.ucEpochNum = results[1]
        self.strNetTime.ucFrameNum = results[2]
        self.strNetTime.usSlotNum  = results[3]
        self.uiMsgNo = results[4]
        self.usRevMsgLength = results[5]
        self.ucJURxMsg = results[6:385]
    

sysWorkTime = SysWorkTime()
